
package cn.dnui_cjy529.dnuifood_16110100529.controller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnui_cjy529.dnuifood_16110100529.MainActivity;
import cn.dnui_cjy529.dnuifood_16110100529.R;
import cn.dnui_cjy529.dnuifood_16110100529.bean.UserBean;

import cn.dnui_cjy529.dnuifood_16110100529.listener.OnRetrofitListener;
import cn.dnui_cjy529.dnuifood_16110100529.model.UserModel;

public class LoginActivity extends BaseActivity implements OnRetrofitListener<UserBean> {
    private EditText username;
    private EditText userpass;
    private Button btn_login;
    private Button btn_reg;
    protected int layout_file= R.layout.activity_login;
    private View tv_reg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {        //这个onCreate跟Base有什么关联吗
        super.onCreate(savedInstanceState);
        setLayout(layout_file);


        initViews();
        initEvents();

      /*  Bundle bundle = new Bundle();       //携带成功注册的用户名参数
        bundle.putString("data","test");
        Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);*/
    }

    public void initViews() {
        setLayout(R.layout.activity_login);
        btn_reg=findViewById(R.id.btn_reg);
        btn_login=findViewById(R.id.btn_log);
        username=findViewById(R.id.username);
        userpass=findViewById(R.id.password);
    }


    public void initEvents() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(username.getText().toString().trim().equals("")||userpass.getText().toString().trim().equals(""))
                {
                    Toast.makeText(LoginActivity.this,"用户名和密码不能为空",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    login();
                }
            }
        });


        tv_reg.setOnClickListener(new View.OnClickListener(){
            @Override
           public void onClick(View v){
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }


        });
    }
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        String et_user=data.getStringExtra("user");
        username.setText(et_user);
    }


    @Override
    void initData() {

    }


//    public void initData() {
//
//    }

    public void login() {
        UserModel userModel = new UserModel();
        userModel.login(username.getText().toString().trim(), userpass.getText().toString().trim(),this);
    }




    @Override
    public void onSuccess(UserBean userid,int flag) {

        if (userid.getUserid().equals("0"))
        {
            Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
        }
        else
        {
            saveUser(userid.getUserid());
            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();

        }
    }

    @Override
    public void onFailure(String string) {
        Toast.makeText(LoginActivity.this,string,Toast.LENGTH_SHORT).show();
    }

    public void saveUser(String user_id)
    {
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putString("username",username.getText().toString());
        int userid = Integer.parseInt(user_id);
        editor.putInt("user_id",userid);
        editor.putString("userpass",userpass.getText().toString());
        editor.commit();//提交
    }




}
